package com.ck.server.controller.test;

import com.ck.common.annotation.OperationLog;
import com.ck.common.bean.ResponseBean;
import com.ck.common.enums.BusinessType;
import com.ck.common.utils.json.JacksonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @author ck
 * @date 2023/3/17 9:25
 */
@RestController
@RequestMapping("rest")
@Api(tags = "RestTemplate测试接口")
public class RstController {



    /**
     * 文件保存地址
     */
    private static final String UPLOAD_PATH = "F:\\Java\\workspace\\my-youth\\uploadPath";




    /**
     * 不带参的Get请求
     */
    @GetMapping("testGet")
    @ApiOperation(value = "不带参的Get请求")
    @OperationLog(title = "不带参的Get请求",businessType = BusinessType.OTHER)
    public ResponseBean testGet(){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("成功");
        return result;
    }

    /**
     * 带参Get请求
     */
    @GetMapping("testGetByRestFul/{id}/{name}")
    @ApiOperation(value = "带参Get请求1")
    public ResponseBean testGetByRestFul(@PathVariable("id") String id,@PathVariable("name") String name){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功,\t"+"参数id:"+id+"参数name:"+name);
        return result;
    }

    @GetMapping("testGetByParam")
    @ApiOperation(value = "带参Get请求2")
    public ResponseBean testGetByParam(@RequestParam("userName") String userName,
                                       @RequestParam("userPwd") String userPwd){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功,\t"+"参数userName:"+userName+"参数userPwd:"+userPwd);
        return result;
    }


    /**
     * 模拟表单请求，post方法测试
     * @return
     */
    @PostMapping(value = "testPostByForm")
    @ApiOperation(value = "模拟表单请求，post方法测试")
    public ResponseBean testPostByForm(@RequestParam("userName") String userName,
                                       @RequestParam("userPwd") String userPwd){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功，方法：testPostByForm，请求参数userName：" + userName + ",userPwd:" + userPwd);
        return result;
    }

    /**
     * 模拟JSON请求，post方法测试
     * @param request
     * @return
     */
    @PostMapping("testPostByJson")
    @ApiOperation(value = "模拟JSON请求，post方法测试")
    public ResponseBean testPostByJson(@RequestBody Map<String,String> request){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功，方法：testPostByJson，请求参数：" + JacksonUtil.objectToJson(request));
        return result;
    }



    /**
     * 文件上传
     * @param uploadFile
     * @return
     */
    @PostMapping("upload")
    @ApiOperation(value = "文件上传")
    public ResponseBean upload(@RequestParam("uploadFile") MultipartFile uploadFile,
                               @RequestParam("userName") String userName) {
            // 在 uploadPath 文件夹中通过用户名对上传的文件归类保存
            File folder = new File(UPLOAD_PATH + userName);
            if (!folder.isDirectory()) {
                folder.mkdirs();
            }

            // 对上传的文件重命名，避免文件重名
            String oldName = uploadFile.getOriginalFilename();
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));

            //定义返回视图
            ResponseBean result = new ResponseBean();
            try {
                // 文件保存
                uploadFile.transferTo(new File(folder, newName));
                result.setCode("200");
                result.setMsg("文件上传成功，方法：upload，文件名：" + newName);
            } catch (IOException e) {
                e.printStackTrace();
                result.setCode("500");
                result.setMsg("文件上传失败，方法：upload，请求文件：" + oldName);
            }
            return result;
        }
}

